package lightswitch.server.code.object;

import lightswitch.server.code.util.TypeOfData;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class MyEntry {
    private String originName;
    private String name; // <alias>.<column>
    private String value;
    private String dataType;
    public MyEntry(){
    	name ="";
    	value="";
    	dataType="";
    	originName = "";
    }
    
    public static MyEntry convertEntry(MyEntry entry, String aliasName){
        MyEntry result = entry;
        String newName = aliasName + "." + result.getOriginName();
        result.setName(newName.toLowerCase());
        return result;
    }
    
    public MyEntry(String name, String value, String type){
    	this.name="";
    	originName="";
    	setName(name);
        this.value = value.toLowerCase();
        dataType = type.toLowerCase();
    }
    public MyEntry(MyEntry en){
        this.name = en.name;
        this.value = en.value;
        this.originName = en.originName;
        dataType = en.dataType;
    }
    
    public String getName(){
        return name;
    }
    
    public String getOriginName(){
        return originName;
    }
    public void setOriginName(String n){
         originName =n;
    }
    
    public String getValue(){
        if (value==null || "".equals(value))
            return "null";
        return value;
    }
    public String getDataType(){
        return dataType;
    }
    
    public void SetDataType(String n){
        dataType = n.toLowerCase();
    }
    public void setName(String n){
        if (originName.equals(name) && !n.contains("."))
            originName = name = n.toLowerCase();
        else
        {
        	String[] str= n.split("\\.");
        	if(str.length==2)
        	{
	        	originName  = str[str.length-1];
	            name = n.toLowerCase();
        	}
        	else
        	{
        		name = n.toLowerCase();
        	}
        }
    }
    
    public void setValue(String v){
        value = v.toLowerCase();
    }
    
    public static Element createElement(Document doc, MyEntry entry){
        Element result = doc.createElement("FIELD");
        result.setAttribute("name", entry.getOriginName());
        result.setAttribute("value", entry.getValue());
        result.setAttribute("type", entry.getDataType());
        return result;
    }
    
    public static MyEntry createEntryFromNode(Element entryElement){
        String name = entryElement.getAttribute("name").toLowerCase();
        String value = entryElement.getAttribute("value").toLowerCase();
        String type = entryElement.getAttribute("type").toLowerCase();
        return new MyEntry(name, value, type);
    }
    
    public String toString(){
        return "("+originName+","+value+")";
    }
    
    public boolean checkEntry(int max){
     // check type
        if (getDataType().equals(TypeOfData.INT)){
            try{
                Integer sampleValue = Integer.parseInt(getValue());
            } catch(Exception e){
                return false;
            }
        } else if (getDataType().equals(TypeOfData.CHAR)){
        }
        // Check range
        
        if (max < (getValue().length()))
            return false;
        return true;
        
    }
}
